package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"medical/models"
	"time"
)

type UserController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) Get() {

}

func (c *UserController) Udl() {
	username := c.GetString("username")
	password := c.GetString("password")
	key := "dl-" + username
	count, _ := models.Rdb.Get(key).Int()
	if count >= 3 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "账号已冻结,请2小时后重试",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "账号不存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		if user.Password == password {
			models.Rdb.Del(key)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "登录成功",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			models.Rdb.Incr(key)
			models.Rdb.Expire(key, time.Hour*2)
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "密码错误",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
	}
}
